
(function (w, u) {

    var ajaxHelper = function () {
        return new ajaxHelper.fn.init();
    }


    ajaxHelper.prototype = ajaxHelper.fn = {

        init: function () {
            return this;
        },

        apiReq: function (config) {
            $.ajax({
                headers: {
                    "token": sessionStorage.getItem("token"),
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                url: config.url,
                method: config.method || 'post',
                async: config.async || true,
                crossDomain: true,
                dataType: 'json',
                data: config.params || null,
                success: config.onSuccess,
                error: function ( ) {
                    console.log("系统错误！");
                }
            });
        },

        uploadImg : function(config){
            $.ajax({
                headers :{
                    token : sessionStorage.getItem("token"),
                },
                url : config.url,
                type : "post",
                data : config.data,
                cache: false,
                contentType: false,    //不可缺
                processData: false,
                success : config.onSuccess || function(response){
                    alert("上传成功");
                    console.log(response);
                },
                error : config.onError || function(response){
                    alert("上传失败");
                    console.log(response);
                }
            })
        },

        getTokenExp : function(token){
            var tokenList = token.split(".");
            return tokenList[1];
        },
        getTokenVerification: function (tokenExp) {
            var b = new Base64();
            var t= b.decode(tokenExp);
            var tok=JSON.parse(t);
            var exp=tok.aud;
            var newDate=new Date().getTime();
            return exp < newDate;
        },
        getRequest: function () {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
    }

    ajaxHelper.fn.init.prototype = ajaxHelper.fn;
    w.ajaxHelper = w.$ajaxHelper = ajaxHelper;
})(window);
